package com.yilu.driver.ui.activity;

import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;

import com.is.common.utils.SPUtils;
import com.is.ui.eventbus.EventCenter;
import com.yilu.driver.DriverConstant;
import com.yilu.driver.R;
import com.yilu.driver.base.BaseActivity;
import com.yilu.driver.entity.LoginResponse;
import com.yilu.driver.mvp.contract.LoginContract;
import com.yilu.driver.mvp.model.LoginModel;
import com.yilu.driver.mvp.presenter.LoginPresenter;

import org.json.JSONException;
import org.json.JSONObject;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;


/**
 * 登录页
 * A login screen that offers login via email/password.
 */
public class LoginActivity extends BaseActivity implements LoginContract.View {


    @Bind(R.id.edit_username)
    EditText editUsername;
    @Bind(R.id.edit_password)
    EditText editPassword;
    @Bind(R.id.btn_login)
    Button btnLogin;
    private LoginPresenter loginPresenter;
    private LoginModel loginModel;

    @Override

    public void initPresenter() {
        loginPresenter = new LoginPresenter(mContext);
        loginModel = new LoginModel();
        loginPresenter.setVM(this, loginModel);
    }

    @Override
    protected boolean isApplySystemBarColor() {
        return true;
    }

    @Override
    protected void getBundleExtras(Bundle extras) {

    }

    @Override
    protected int getContentViewLayoutID() {
        return R.layout.activity_login;
    }

    @Override
    protected void onEventComming(EventCenter eventCenter) {

    }

    @Override
    protected void initViewsAndEvents() {

    }

    @Override
    protected boolean isApplyStatusBarTranslucency() {
        return false;
    }

    @Override
    protected boolean isBindEventBusHere() {
        return false;
    }

    @Override
    protected boolean toggleOverridePendingTransition() {
        return false;
    }

    @Override
    protected TransitionMode getOverridePendingTransitionMode() {
        return null;
    }

    @Override
    public void returnLogin(LoginResponse loginResponse) {
        if (loginResponse.getStatus().equals(DriverConstant.SUCCESS_STATUS)) {//登录成功
            showToast(loginResponse.getTipMsg());
            SPUtils.put(mContext, "isLogin", true);
            SPUtils.put(mContext, "id", loginResponse.getData().getId());
            SPUtils.put(mContext, "status", loginResponse.getData().getStatus());
            readyGoThenKill(MainActivity.class);
        } else {//登录失败
            showToast(loginResponse.getTipMsg());
        }
        editPassword.setText("");
    }

    @OnClick(R.id.btn_login)
    public void onClick() {
        String username = editUsername.getText().toString();//用户名
        String password = editPassword.getText().toString();//密码
        /**
         * 判断用户名、密码是否为空
         */
        if (username.isEmpty()) {
            editUsername.setError("请输入用户名");
            editUsername.requestFocus();
        } else if (password.isEmpty()) {
            editPassword.setError("请输入密码");
            editPassword.requestFocus();
        } else {
            login(username, password);//发送登录请求
        }
    }

    /**
     * 发送登录请求
     *
     * @param username 用户名
     * @param password 密码
     */
    public void login(String username, String password) {
        JSONObject json = new JSONObject();
        try {
            json.put("account", username);
            json.put("password", password);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        loginPresenter.sendLogin(json);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }
}



